<template>
  <div>
    <el-dialog
      v-model="open"
      :close-on-click-modal="false"
      :title="title"
      width="500px"
      append-to-body
    >
      <el-form
        ref="editForm"
        :model="form"
        inline
        label-width="80px"
        :rules="rules"
      >
        <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入岗位名称"></el-input>
        </el-form-item>
        <el-form-item label="岗位编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入岗位编码"></el-input>
        </el-form-item>
        <el-form-item label="岗位顺序" prop="postSort">
          <el-input-number
            v-model="form.postSort"
            controls-position="right"
            :min="0"
            type="number"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="岗位状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dicts[DictDirection.是否禁用字典]" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, watch, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'
import { listPost, getPost, delPost, addPost, updatePost } from '@/api/system/post'
import useDicts, { DictDirection } from '@/store/modules/dicts'

const dicts = useDicts()

let form = ref({
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    postSort: 0,
    status: undefined,
    remark: undefined,
  }),
  // 表单校验
  rules = {
    postName: [{ required: true, message: '岗位名称不能为空', trigger: 'blur' }],
    postCode: [{ required: true, message: '岗位编码不能为空', trigger: 'blur' }],
    postSort: [
      {
        required: true,
        message: '岗位顺序不能为空',
        trigger: 'blur',
      },
      {
        validator: (rule, value, callback) => {
          if (!(/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value)) {
            callback(new Error('请输入数字值'))
          } else {
            callback()
          }
        },
      },
    ],
  },
  // 标题
  title = ref(''),
  // 模态框显示
  open = ref(false),
  // 新增修改表单
  editForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()
const emit = defineEmits(['changeList'])
// 重置新增修改列表
const reset = () => {
  form.value = {
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    postSort: 0,
    status: '0',
    remark: undefined,
  }
  $resetForm(editForm.value)
}

// 新增修改——确定
const submitForm = () => {
  $validateForm(editForm.value, () => {
    if (form.value.postId != undefined) {
      updatePost(form.value).then(res => {
        if (res.code == 200) {
          ElMessage.success('修改成功')
          emit('changeList')
          reset()
          open.value = false
        }
      })
    } else {
      addPost(form.value).then(res => {
        if (res.code == 200) {
          ElMessage.success('新增成功')
          emit('changeList')
          reset()
          open.value = false
        }
      })
    }
  })
}
// 修改新增——取消
const cancel = () => {
  open.value = false
}
const getList = (id) => {
  getPost(id)
  .then((res) => {
    if (res.code === 200) {
      const resList = JSON.parse(JSON.stringify(res.data))
      resList.postSort = +resList.postSort
      form.value = resList
      console.log(form.value)
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((err) => {
    console.log(err)
    ElMessage.error(err)
  })
}
dicts.setDicts(DictDirection.是否禁用字典)
watch(open, (v) => {
  if (!v) {
    reset()
  }
})
defineExpose({
  open,
  title,
  getList,
})
</script>
